<?php

!defined('IN_VIFRAME') && die('Access Denied.');

class COption extends TClass implements ArrayAccess {
	
	public function load($key, $optionKey) {
		if (is_int($optionKey)) {
			$condition = "`OptionId` = '$optionKey'";
		} else {
			$condition = "`OptionKey` = '$optionKey'";
		}
		
		Db::select('Option', 'OptionValue', $condition);
		$data = Db::fetch();
		
		return $this->simpleSet($key, unserialize($data['OptionValue']));
	}
	
	public function simpleLoad($key, $optionKey) {
		if (is_int($optionKey)) {
			$condition = "`OptionId` = '$optionKey'";
		} else {
			$condition = "`OptionKey` = '$optionKey'";
		}
		
		Db::select('Option', 'OptionValue', $condition);
		$data = Db::fetch();
		
		return $this->simpleSet($key, $data['OptionValue']);
	}
	
	public function insert($key, $value, $id = -1) {
		$option = array(
			'OptionKey'   => $key,
			'OptionValue' => serialize($value)
		);
		if ($id > 0) $option['OptionId'] = $id;
		
		return Db::insert('Option', $value);
	}
	
	public function update($key, $optionKey) {
		if (is_int($optionKey)) {
			$condition = "`OptionId` = '$optionKey'";
		} else {
			$condition = "`OptionKey` = '$optionKey'";
		}
		
		return Db::update('Option', array('OptionValue' => serialize(Config::get("Cms.$key"))), $condition);
	}
	
	public function updateKey($id, $key) {
		return Db::update('Option', array('OptionKey' => $key), array('OptionId' => $id));
	}
	
	public function updateId($key, $id) {
		return Db::update('Option', array('OptionId' => $id), array('OptionKey' => $key));
	}
	
	public function delete($optionkey) {
		if (is_int($optionKey)) {
			$condition = "`OptionId` = '$optionKey'";
		} else {
			$condition = "`OptionKey` = '$optionKey'";
		}
		
		return Db::delete('Option', $condition);
	}
	
	public function has($key) {
		return Config::has("Cms.$key");
	}
	
	public function simpleHas($key) {
		return Config::simpleHas("Cms.$key");
	}
	
	public function get($key) {
		return Config::get("Cms.$key");
	}
	
	public function simpleGet($key) {
		return Config::simpleGet("Cms.$key");
	}
	
	public function set($key, $value = VI_CONFIG_RECURSIVESET) {
		return Config::set("Cms.$key", $value);
	}
	
	public function simpleSet($key, $value) {
		return Config::simpleSet("Cms.$key", $value);
	}
	
	public function remove($key) {
		return Config::remove("Cms.$key");
	}
	
	public function simpleRemove($key) {
		return Config::simpleRemove("Cms.$key");
	}
	
	public function offsetExists($key) {
		return Config::simpleHas("Cms.$key");
	}
	
	public function offsetGet($key) {
		return Config::simpleGet("Cms.$key");
	}
	
	public function offsetSet($key, $value) {
		return Config::simpleSet("Cms.$key", $value);
	}
	
	public function offsetUnset($key) {
		return Config::simpleRemove("Cms.$key");
	}
}